package ca.bcit.comp3910;

import java.io.Serializable;
import java.util.ArrayList;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import ca.bcit.comp3910.access.TimesheetManager;
import ca.bcit.comp3910.model.TimesheetRow;

/**
 * View class to interact between Timesheet Manager and the JSF modifyTimesheet
 * 
 * @author Ivy Cheung, Stacy Mitchell
 * @version 2.0
 * 
 */
@SuppressWarnings("serial")
@Named
@SessionScoped
public class ModifyTimesheet implements Serializable {
    /**
     * Time Manager bean
     */
    @Inject
    TimesheetManager tm;

    /**
     * week number
     */
    private int weekNum;
    /**
     * week ending on this date
     */
    private String weekDate;
    /**
     * array list of timesheet row (data)
     */
    private ArrayList<TimesheetRow> rows;
    
    /**
     * For the back button
     * 
     * @return string to head to modifyTimesheet
     */
    public String back() {
    	tm.setViewTS(false);
        return "modifyTimesheet";
    }

    /**
     * For the save button
     * 
     * @return returns null : refreshes page
     */
    public String saveAction() {
        tm.saveRow(rows);
        return null;
    }
    /**
     * Set rows
     * 
     * @param rows
     */
    public void setRows(ArrayList<TimesheetRow> rows) {
        this.rows = rows;
    }
    /**
     * Gets rows from timesheetrow array, to generate the dataTable
     * 
     * @return timesheetrow object
     */
    public ArrayList<TimesheetRow> getRows() {
    	rows = tm.getTsrowList();
        return rows;
    }

    /**
     * Adds row in the datatable
     */
    public void addRow() {
        rows.add(new TimesheetRow());
    }

    /**
     * Delete row in the datatable, stops when there is 5 rows
     */
    public void deleteRow() {
        if (rows.size() > 5) {
            rows.remove(rows.size() - 1);
        }
    }
    
    /**
     * Get Week Number
     * @return week number
     */
    public int getWeekNum() {
    	this.weekNum = tm.getWeekNum();
    	return weekNum;
    }
    
    /**
     * Get Week Date
     * @return week date
     */
    public String getWeekDate() {
    	this.weekDate = tm.getWeekDate();
    	return weekDate;
    }
    /**
     * Set Week Date
     */
    public void setWeekDate(String weekDate) { this.weekDate = weekDate; }

    /**
     * Get total of the column total
     * 
     * @return n: sum
     */
    public double getColTotal() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getTotal();
        }
        return n;
    }

    /**
     * Get total of Saturday column
     * 
     * @return n: sum
     */
    public double getTotalSat() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getSat();
        }
        return n;
    }

    /**
     * Get total of Sunday column
     * 
     * @return n: sum
     */
    public double getTotalSun() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getSun();
        }
        return n;
    }

    /**
     * Get total of Monday column
     * 
     * @return n: sum
     */
    public double getTotalMon() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getMon();
        }
        return n;
    }

    /**
     * Get total of Tuesday total
     * 
     * @return n: sum
     */
    public double getTotalTue() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getTue();
        }
        return n;
    }

    /**
     * Get total of Wednesday column
     * 
     * @return n: sum
     */
    public double getTotalWed() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getWed();
        }
        return n;
    }

    /**
     * Get total of Thursday column
     * 
     * @return n: sum
     */
    public double getTotalThu() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getThu();
        }
        return n;
    }

    /**
     * Get total of Friday column
     * 
     * @return n: sum
     */
    public double getTotalFri() {
        double n = 0;
        for (TimesheetRow temp : rows) {
            n += temp.getFri();
        }
        return n;
    }
}
